<?php

/**
 * This is the model class for table "zy_appuser".
 *
 * The followings are the available columns in table 'zy_appuser':
 * @property integer $id
 * @property integer $userid
 * @property string $send_id
 * @property string $imei
 * @property integer $platform
 * @property integer $app_id
 */
class Appuser extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Appuser the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_appuser';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userid', 'checkAppuserAlready'),//checkAppuserAlready是下面写的方法
			array('imei', 'checkAppuserAlready'),//checkAppuserAlready是下面写的方法
			array('app_id', 'checkAppuserAlready'),//checkAppuserAlready是下面写的方法
			array('userid, platform, app_id', 'numerical', 'integerOnly'=>true),
			array('send_id, imei', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, userid, send_id, imei, platform, app_id', 'safe', 'on'=>'search'),
		);
	}

	//检查userid,imei,app_id有没有存在
	public function checkAppuserAlready($attribute,$params)
	{
		$modelselect=Appuser::model()->find('userid=:userid AND imei=:imei AND app_id=:app_id',
									array(':userid'=>$this->userid,
										':imei'=>$this->imei,
										':app_id'=>$this->app_id,
										))	;
		if($modelselect!=null)
		{
		 $this->addError($attribute, $attribute.'已存在');
		}					
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('base','AppuserID'),
			'userid' => Yii::t('base','Userid'),
			'send_id' => Yii::t('base','SendId'),
			'imei' => Yii::t('base','Imei'),
			'platform' => Yii::t('base','Platform'),
			'app_id' => Yii::t('base','App'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('userid',$this->userid);
		$criteria->compare('send_id',$this->send_id,true);
		$criteria->compare('imei',$this->imei,true);
		$criteria->compare('platform',$this->platform);
		$criteria->compare('app_id',$this->app_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}